/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
var toggle = false;
var selectToggle = false;

function selectDate( type, args, obj ) {
	if( !selectToggle ) {
		var date = args[0][0];
		date = date[1] + "/" + date[2] + "/" + date[0];
		
		toggle = true;
		calendar.deselect( date );
		calendar.render();
	}
	else {
		selectToggle = false;
	}
}

function deselectDate( type, args, obj ) {
	if( !toggle ) {
		var date = args[0][0];
		
		var dateYUI = date[1] + "/" + date[2] + "/" + date[0];
		var day = "" + date[2];
		while ( day.length < 2 ) {
			day = '0' + day;
		}
		var month = "" + date[1];
		while ( month.length < 2 ) {
			month = '0' + month;
		}
		var year = "" + date[0];
		while ( year.length < 4 ) {
			year = '0' + year;
		}
		date = day + "-" + month + "-" + year;

		var hiddenElement = document.getElementById( "history-item-list-hidden" );
		hiddenElement.innerHTML = date;
		
		var element = document.getElementById( "history-item-list-title" );
		element.innerHTML = 'Loading...';
		var element = document.getElementById( "history-item-list" );
		element.innerHTML = "";
		
		YAHOO.util.Connect.asyncRequest( 'GET', "get_history.php?date=" + date, dateSelectCallback );
		selectToggle = true;
		calendar.select( dateYUI );
		calendar.render();
	}
	toggle = false;
}

var responseDateSuccess = function( o ) {
	var hiddenElement = document.getElementById( "history-item-list-hidden" );
	var element = document.getElementById( "history-item-list-title" );
	element.innerHTML = 'Reported events on ' + hiddenElement.innerHTML;
	var element = document.getElementById( "history-item-list" );
	element.innerHTML = o.responseText;
};

var responseDateFailure = function( o ) {
	alert( "Error fetching history items on date, error message: " + o.responseText );
};

var dateSelectCallback = {
	success: responseDateSuccess, 
	failure: responseDateFailure
};

function selectTag( tag, positive ) {
	var tagfield = document.getElementById( 'tag-field' );
	tagfield.value = tag;
	var positiveBox = document.getElementById( 'positive-checkbox' );
	var negativeBox = document.getElementById( 'negative-checkbox' );
	positiveBox.checked = positive;
	negativeBox.checked = !positive;
}

function replyTo( comment_id ) {
	var link = null;
	var comment = null;
	var container = document.getElementById( 'comments-container' );
	var links = YAHOO.util.Dom.getElementsByClassName( 'reply-to-link', 'a', container );
	YAHOO.util.Dom.removeClass( links, 'hidden' );
	
	if( comment_id != -1 ) {
		var comment = document.getElementById( 'comment-' + comment_id );
		link = YAHOO.util.Dom.getElementsByClassName( 'reply-to-link', 'a', comment );
	}

	var write = document.getElementById( 'write-comment' );
	write.parentNode.removeChild( write );
	if( comment_id != -1 ) {
		comment.appendChild( write );
	}
	else {
		container.appendChild( write );
	}
	var parent_id = document.getElementById( 'parent_id' );
	parent_id.value = comment_id;
	var replyToHistory = document.getElementById( 'reply-to-history' );
	if( comment_id != -1 ) {
		YAHOO.util.Dom.removeClass( replyToHistory, 'hidden' );
		YAHOO.util.Dom.addClass( link[0], 'hidden' );
	}
	else {
		YAHOO.util.Dom.addClass( replyToHistory, 'hidden' );
	}
}

function loadNewTags() {
	var type = document.getElementById( 'tag-type-select' );
	type = type.value;
	var history_id = document.getElementById( 'history_id' );
	history_id = history_id.innerHTML;
	var container = document.getElementById( 'tag-container' );
	container.innerHTML = 'Loading tags...';
	YAHOO.util.Connect.asyncRequest( 'GET', "get_tags.php?type=" + type + "&history_id=" + history_id, tagLoadCallback );
}

var tagLoadSuccess = function( o ) {
	var container = document.getElementById( 'tag-container' );
	container.innerHTML = o.responseText;
};

var tagLoadFailure = function( o ) {
	alert( "Error loading tags, error message: " + o.responseText );
};

var tagLoadCallback = {
	success: tagLoadSuccess, 
	failure: tagLoadFailure
};